package net.grapsus.arduino;

import java.util.*;
import java.io.*;

import org.objectweb.asm.*;
import org.objectweb.asm.commons.*;

public class ApiParser implements ClassVisitor
{
	private HashMap<String,Function> functions;
	private HashMap<String,Variable> variables;
	private HashMap<String,Variable> staticVariables;

	public ApiParser() 
	{
		functions = new HashMap<String,Function>();
		variables = new HashMap<String,Variable>();
		staticVariables = new HashMap<String,Variable>();
	}

	public HashMap<String,Function> getFunctions()
	{
		return functions;
	}
	public HashMap<String,Variable> getVariables()
	{
		return variables;
	}
	public HashMap<String,Variable> getStaticVariables()
	{
		return staticVariables;
	}

	public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions)
	{
		if(((access & Opcodes.ACC_PUBLIC) != 0) && ((access & Opcodes.ACC_STATIC) == 0) && ! name.equals("<init>"))
		{
			System.out.println("Registered method " + name + " " + desc);
			functions.put(name, new Function(desc));
		}
		return null;
	}

	public FieldVisitor	visitField(int access, String name, String desc, String signature, Object value)
	{
		if(((access & Opcodes.ACC_PUBLIC) != 0) && ((access & Opcodes.ACC_STATIC) == 0))
		{
			System.out.println("Registered field " + name + " " + desc);
			variables.put(name, new Variable(desc));
		}
		else if(((access & Opcodes.ACC_PUBLIC) != 0) && ((access & Opcodes.ACC_STATIC) != 0))
		{
			System.out.println("Registered static field " + name + " " + desc);
			staticVariables.put(name, new Variable(desc));
		}
		return null;
	}

	/* we don't care about those */
	public void 	visit(int version, int access, String name, String signature, String superName, String[] interfaces){}
	public AnnotationVisitor 	visitAnnotation(String desc, boolean visible){ return null; }
	public void 	visitAttribute(Attribute attr){}
	public void 	visitEnd(){}
	public void 	visitInnerClass(String name, String outerName, String innerName, int access){}
	public void 	visitOuterClass(String owner, String name, String desc){}
	public void 	visitSource(String source, String debug){}
}
